package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysScenicArea;
import com.mars.module.admin.entity.SysSpotReview;
import com.mars.module.admin.entity.SysTravelRoute;
import com.mars.module.admin.mapper.SysScenicAreaMapper;
import com.mars.module.admin.mapper.SysSpotReviewMapper;
import com.mars.module.admin.mapper.SysTravelRouteMapper;
import com.mars.module.system.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysScenicSpotRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysScenicSpotMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysScenicSpot;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysScenicSpotService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 景点业务层处理
 *
 * @author mars
 * @date 2024-03-25
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysScenicSpotServiceImpl implements ISysScenicSpotService {

    private final SysScenicSpotMapper baseMapper;
    private final SysScenicAreaMapper areaMapper;
    private final SysSpotReviewMapper reviewMapper;
    private final SysTravelRouteMapper routeMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysScenicSpot add(SysScenicSpotRequest request) {
        SysScenicSpot entity = SysScenicSpot.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysScenicSpotRequest request) {
        SysScenicSpot entity = SysScenicSpot.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysScenicSpot getById(Long id) {
        SysScenicSpot scenicSpot = baseMapper.selectById(id);
        SysScenicArea area = areaMapper.selectById(scenicSpot.getAreaId());
        scenicSpot.setAreaName(area == null ? "" : area.getName());
        scenicSpot.setReviewList(reviewMapper.selectList(new QueryWrapper<SysSpotReview>().eq("spot_id", scenicSpot.getId())));
        return scenicSpot;
    }

    @Override
    public PageInfo<SysScenicSpot> pageList(SysScenicSpotRequest request) {
        Page<SysScenicSpot> page = new Page<>(request.getPageNo(), request.getPageSize());
        SysTravelRoute route = routeMapper.selectById(request.getRouteId());
        if (route != null) {
            request.setAreaId(route.getAreaId());
        }
        LambdaQueryWrapper<SysScenicSpot> query = this.buildWrapper(request);
        IPage<SysScenicSpot> pageRecord = baseMapper.selectPage(page, query);
        List<SysScenicSpot> records = pageRecord.getRecords();
        List<SysScenicSpot> recordsNew = new ArrayList<>();
        for (SysScenicSpot scenicSpot : records) {
            SysScenicArea area = areaMapper.selectById(scenicSpot.getAreaId());
            scenicSpot.setAreaName(area == null ? "" : area.getName());
            scenicSpot.setProvinceId(area.getProvinceId());
            scenicSpot.setReviewList(reviewMapper.selectList(new QueryWrapper<SysSpotReview>().eq("spot_id", scenicSpot.getId())));
            recordsNew.add(scenicSpot);
        }
        if (request.getProvinceId() != null) {
            recordsNew = recordsNew.stream().filter(e -> e.getProvinceId() == request.getProvinceId()).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysScenicSpot> list(SysScenicSpotRequest request) {
        LambdaQueryWrapper<SysScenicSpot> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysScenicSpot> buildWrapper(SysScenicSpotRequest param) {
        LambdaQueryWrapper<SysScenicSpot> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysScenicSpot::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getIntroduce())) {
            query.like(SysScenicSpot::getIntroduce, param.getIntroduce());
        }
        if (StringUtils.isNotBlank(param.getPicture())) {
            query.like(SysScenicSpot::getPicture, param.getPicture());
        }
        if (param.getAreaId() != null) {
            query.eq(SysScenicSpot::getAreaId, param.getAreaId());
        }
        return query;
    }

}
